//const gulp = require('gulp');
const {src,dest,parallel,series} = require('gulp');
// less转css
const less = require('gulp-less');
// 压缩css
const cleancss = require('gulp-clean-css');
// 重命名
const rename = require('gulp-rename');
// 给css加浏览器前缀
const autoprefixer = require('gulp-autoprefixer');
// 引入babel
const babel = require('gulp-babel');
// 压缩js
const uglify = require('gulp-uglify');
//引入压缩html
const htmlmin = require('gulp-htmlmin');
// 引入压缩图片
const imagemin = require('gulp-imagemin');
// 清除文件
const del = require('del');
// 引入browsersync
const browserSync = require('browser-sync');
const bs = browserSync.create();
// 声明gulp任务
const style = ()=>{
  return src('src/css/index.less',{
    base:'src'
  }).pipe(less())
  .pipe(autoprefixer())
  .pipe(cleancss())
  .pipe(rename({
    "extname":".min.css"
  }))
  .pipe(dest('dist'));
}

// 声明脚本构建任务
const script1 = ()=>{
  return src('src/js/jquery-1.12.4.js')
  .pipe(dest('dist/js'))
};
const script2 = ()=>{
  return src('src/js/template-native.js')
  .pipe(dest('dist/js'))
};
const script = ()=>{
  return src('src/js/index.js',)
  .pipe(babel({
    presets: ['babel-preset-env']
  }))
  .pipe(uglify())
  .pipe(rename({
    "extname":".min.js"
  }))
  .pipe(dest('dist/js'))
};
// 声明页面构建任务 
const html = ()=>{
  return src('src/index.html')
  // 是否折叠空白字符 默认是false
  .pipe(htmlmin({ collapseWhitespace: true ,
    minifyCSS:true,
    minifyJS:true
  }))
  .pipe(dest('dist'))
};
// 声明图片构建任务
const image = ()=>{
  return src('src/img/**',{
    base:'src'
  })
  .pipe(imagemin([
    imagemin.mozjpeg({quality: 75, progressive: true}),
    imagemin.optipng({optimizationLevel: 5})
  ]))
  .pipe(dest('dist'))
};
// 清除文件
const clean = ()=>{
  return del(['dist'])
};
// 服务器发布任务
const serve = ()=>{
  // 初始化服务
  bs.init({
    server:{
      // 指定服务启动目录
      baseDir:'./dist'
    }
  });

};
// 并行执行
const build = parallel(style,script1,script2,script,html,image);
// 串行
const dev = series(clean,build,serve);
module.exports = {
  style,
  script,
  html,
  image,
  build,
  clean,
  serve,
  dev,
  
}